#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

// 96.82 %
class Solution
{
  public:
	bool containsNearbyDuplicate(vector<int> &nums, int k)
	{
		unordered_set<int> record;
		for (int i = 0; i < nums.size(); i++)
		{
			if (record.find(nums[i]) != record.end())
				return true;
			record.insert(nums[i]);
			if (record.size() == k + 1)
			{
				record.erase(nums[i - k]);
			}
		}
		return false;
	}
};

int main()
{
	int nums[] = {1, 2, 3, 1}, k = 3;
	int len = sizeof(nums) / sizeof(nums[0]);
	vector<int> myVector(nums, nums + len);
	cout << Solution().containsNearbyDuplicate(myVector, k) << endl;
	return 0;
}
